package com.lagerplan.misc;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.sql.Date;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * Manage Application Properties
 * Read and Load config settings
 * define resources at one point within the application
 * 
 * @author Q243471
 *
 */
public class ApplicationProperties {

	// Log errors -> setup Logging before setting up App Props
	private static Logger logger = Logger.getLogger(ApplicationProperties.class);
	//private static final String APPLICATION_PROPERTIES_RES = LogLoader.CLASSES_HOME + "config/applicationproperties.xml";		
	// Use relative file path
	private static final String APPLICATION_PROPERTIES_RES = "config/applicationproperties_work.xml";		
	// *** keys to retrieve the values retrieved from xml config file ***
	public static final String KEY_ICON_ANIMATION = "ra.workflow.icon.animation";
	public static final String KEY_ICON_TASK_DELETE = "ra.workflow.icon.task.delete";
	public static final String KEY_ICON_TASK_MODIFIED = "ra.workflow.icon.task.modified";
	public static final String KEY_ICON_TASK_NEW = "ra.workflow.icon.task.new";
	public static final String KEY_ICON_WORKFLOW_START = "ra.workflow.icon.workflow.start";
	public static final String KEY_ICON_WORKFLOW_STOP = "ra.workflow.icon.workflow.stop";
	
	public static final String KEY_WORKFLOW_ROOTDIR = "ra.workflow.rootDir";
	public static final String KEY_ANIMPROJ_ROOTDIR = "ra.anim.rootDir";
	public static final String KEY_WORKFLOW_SAVEDIR = "ra.workflow.saveDir";
	public static final String KEY_WORKFLOW_SAVE_TEMPDIR = "ra.workflow.saveTempDir";
	public static final String KEY_VIRTUALCAR_ROOTDIR = "virtualcar.rootDir";
	public static final String KEY_DEFINITION_FILE_TOOLS = "ra.anim.tools";
	public static final String KEY_DEFINITION_FILE_TEMPLATES = "ra.anim.templates";
	public static final String KEY_ARVISEDIT_EXECUTABLE = "arvisedit.exec";
	
	// Pfad zu den Label Grafiken der Fahrzeuge, im Bereich "Quelle" - "Ziel" der GUI
	public static final String KEY_IMAGE_PATH_ECODE = "resources/icon_src_";
	public static final String KEY_IMAGE_PATH_ECODE_IMG_TYPE = ".png";
	
	// stores the entries
	static public Properties entries = new Properties();
	
	public ApplicationProperties() {
	}
	
	/**
	 * Write properties to xml file
	 *
	 */
	public static void saveProperties() {
		
		FileOutputStream propOutFile = null;
		try {
			propOutFile = new FileOutputStream( APPLICATION_PROPERTIES_RES );
			entries.storeToXML(propOutFile, "Written to File " );
			propOutFile.close();
		}
		catch(Exception e) {
			logger.error("Fehler beim Schreiben der App Properties: " + e.getMessage());
		}
	}
	
	/**
	 * Read properties from xml file
	 *
	 */
	public static void initialize() {
		
		FileInputStream propInFile = null;
		try {
			propInFile = new FileInputStream(APPLICATION_PROPERTIES_RES);
			entries.loadFromXML( propInFile );
			logger.info("Found properties in prp file: " + entries.toString() );
			propInFile.close();
		}
		catch(Exception e) {
			logger.error("Fehler beim Einlesen der App Properties ["+APPLICATION_PROPERTIES_RES+"]: " + e.getMessage());
		}
	}
	
	
}
